import getData from '../../service/getData2.js'
import * as types from '../types'
import * as types1 from '../appendType'

const state = {
	shopList:[],
	shopPage:1,
	shopPageLimit:6,
	
    scroll: true //避免快速滚动，无限拉取数据源
}
var me=this;
const actions = {

    //我的道具列表
	myProp:function({commit,rootState,getters },params){
        
		let page = {"aoData":"[{\"name\":\"iDisplayStart\",\"value\":"+state.shopPage+"}," +
        "{\"name\":\"iDisplayLength\",\"value\":"+state.shopPageLimit+"}]"
        };
            
        params.data = Object.assign(page,params.data);
        return new Promise((resolve, reject) => {

            if(rootState.coupon.scroll) {

                commit(types.GET_SCORLL_STATUS,false);//避免快速滚动
                getData.myProp(params, getters.userNum).then(function (rep) {

                    commit(types.GET_SCORLL_STATUS,true);//避免快速滚动
                    resolve(rep);
                    if(rep.respCode == "00"){
                        commit(types1.shop_list,rep);
                        if(rep.content.result.length == 0) {
                            return;
                        }
                        state.shopPage++;
                    }
                }).catch(function(rep){
                    reject(rep)
                });
            }
        });
	},
    //道具商城列表
    shopProp:function({commit,rootState,getters },params){
        
        let page = {"aoData":"[{\"name\":\"iDisplayStart\",\"value\":"+state.shopPage+"}," +
        "{\"name\":\"iDisplayLength\",\"value\":"+state.shopPageLimit+"}]"
        };
            
        params.data = Object.assign(page,params.data);
        return new Promise((resolve, reject) => {

            if(rootState.coupon.scroll) {

                commit(types.GET_SCORLL_STATUS,false);//避免快速滚动
                getData.shopProp(params, getters.userNum).then(function (rep) {

                    commit(types.GET_SCORLL_STATUS,true);//避免快速滚动
                    resolve(rep);
                    if(rep.respCode == "00"){
                        commit(types1.shop_list,rep);
                        if(rep.content.result.length == 0) {
                            return;
                        }
                        state.shopPage++;
                    }
                }).catch(function(rep){
                    reject(rep)
                });
            }
        });
    }
}
const getters = {

}
const mutations = {
	//列表
    [types1.shop_list](state,rep){
        if(!rep){
            state.shopList = [];
            return
        }
        if (state.shopPage <= 1) {
            state.shopList = rep.content.result;
        } else {
            state.shopList = state.shopList.concat(rep.content.result);
            
        }
    },
    //分页
    [types1.shop_page](state,num){
        state.shopPage = num;
    }
    
}

export default {
    state,
    actions,
    getters,
    mutations
}